package com.villaday.idf.activity;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import androidx.viewpager.widget.ViewPager;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.appcompat.widget.Toolbar;
import android.text.Html;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.atuan.datepickerlibrary.CalendarUtil;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.villaday.idf.R;
import com.villaday.idf.activity3.CheckInvoiceActivity;
import com.villaday.idf.activity3.InvoiceApplicationActivity;
import com.villaday.idf.adapter3.OrderDeatailsPopuAdapter;
import com.villaday.idf.adapter3.OrderDetailsAdapter;
import com.villaday.idf.adapter3.PopupOrderAdapter;
import com.villaday.idf.base.BaseActivity;
import com.villaday.idf.base.BaseDialog;
import com.villaday.idf.custom.CommonPopupWindow;
import com.villaday.idf.custom.ConfirmReturnDialog;
import com.villaday.idf.custom.LoadDialog;
import com.villaday.idf.custom.PayDialog;
import com.villaday.idf.custom.bannerUtil.BannerUtils;
import com.villaday.idf.custom.pop.FaPiaoInfoPop;
import com.villaday.idf.model.PayEntity;
import com.villaday.idf.model.PayMent;
import com.villaday.idf.model3.ChangeDetailsPostEntity;
import com.villaday.idf.model3.DataEntity;
import com.villaday.idf.model3.FaPiaoconfigurationsEntity;
import com.villaday.idf.model3.MemberGuestChangeEntity;
import com.villaday.idf.model3.OrderDetailsEntity;
import com.villaday.idf.util.Constant;
import com.villaday.idf.util.LogUtil;
import com.villaday.idf.util.LogeUtil;
import com.villaday.idf.util.OkHttpUtil;
import com.villaday.idf.util.SharedPreferencesUtil;
import com.villaday.idf.util.ToastUtil;
import com.villaday.idf.util.ToolBarUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 订单详情
 */
public class OrderDetailsActivity extends BaseActivity {


    @BindView(R.id.toolbar_back)
    ImageButton toolbarBack;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.toolbar_next)
    TextView toolbarNext;
    @BindView(R.id.Line_view)
    View LineView;
    @BindView(R.id.toolbar_text)
    Toolbar toolbarText;
    @BindView(R.id.iv_order_type)
    ImageView ivOrderType;
    @BindView(R.id.tv_order_title)
    TextView tvOrderTitle;
    @BindView(R.id.tv_order_content)
    TextView tvOrderContent;
    @BindView(R.id.tv_order_right)
    TextView tvOrderRight;
    @BindView(R.id.tv_order_content_title)
    TextView tvOrderContentTitle;
    @BindView(R.id.tv_order_content_eng)
    TextView tvOrderContentEng;
    @BindView(R.id.tv_order_content_left)
    TextView tvOrderContentLeft;
    @BindView(R.id.tv_order_content_right)
    TextView tvOrderContentRight;
    @BindView(R.id.tv_order_content_time)
    TextView tvOrderContentTime;
    @BindView(R.id.tv_order_content_day)
    TextView tvOrderContentDay;
    @BindView(R.id.tv_order_content_num)
    TextView tvOrderContentNum;
    @BindView(R.id.tv_od_details_desc)
    TextView tvOdDetailsDesc;
    @BindView(R.id.rv_order_user_left)
    RecyclerView rvOrderUserLeft;
    @BindView(R.id.tv_order_phone_left)
    TextView tvOrderPhoneLeft;
    @BindView(R.id.tv_order_user_phone)
    TextView tvOrderUserPhone;
    @BindView(R.id.tv_order_email_left)
    TextView tvOrderEmailLeft;
    @BindView(R.id.tv_order_user_email)
    TextView tvOrderUserEmail;
    @BindView(R.id.tv_order_more)
    TextView tvOrderMore;
    @BindView(R.id.iv_order_more)
    ImageView ivOrderMore;
    @BindView(R.id.tv_order_bottom_num_left)
    TextView tvOrderBottomNumLeft;
    @BindView(R.id.tv_order_bottom_num_right)
    TextView tvOrderBottomNumRight;
    @BindView(R.id.tv_order_bottom_num)
    TextView tvOrderBottomNum;
    @BindView(R.id.tv_order_bottom_time_left)
    TextView tvOrderBottomTimeLeft;
    @BindView(R.id.tv_order_bottom_time)
    TextView tvOrderBottomTime;
    @BindView(R.id.tv_order_bottom_type_left)
    TextView tvOrderBottomTypeLeft;
    @BindView(R.id.tv_order_bottom_type)
    TextView tvOrderBottomType;
    @BindView(R.id.tv_order_bottom_more_left)
    TextView tvOrderBottomMoreLeft;
    @BindView(R.id.tv_order_bottom_more)
    TextView tvOrderBottomMore;
    @BindView(R.id.tv_order_delete)
    TextView tvOrderDelete;
    @BindView(R.id.tv_order_phone)
    TextView tvOrderPhone;
    @BindView(R.id.tv_order_pay)
    TextView tvOrderPay;
    @BindView(R.id.tv_od_money)
    TextView tvOdMoney;
    @BindView(R.id.ll_oda)
    LinearLayout llOl;
    @BindView(R.id.vw_od_more_top)
    View vwOdMoreTop;
    @BindView(R.id.tv_od_more)
    TextView tvOdMore;
    @BindView(R.id.vw_od_more_bottom)
    View vwOdMoreBottom;
    @BindView(R.id.ll_order_user_left)
    LinearLayout llOrderUserLeft;

    @BindView(R.id.ll_order_fapiaoInfo)
    LinearLayout ll_order_fapiaoInfo;

    @BindView(R.id.tv_order_fapiaoInfo)
    TextView tv_order_fapiaoInfo;
    @BindView(R.id.tv_order_fapiao)
    TextView tv_order_fapiao;


    @BindView(R.id.tv_fapiaoName)
    TextView tv_fapiaoName;
    @BindView(R.id.tv_type)
    TextView tv_type;
    private String mOrderNo = "V190808-540604-00";
    private List<OrderDetailsEntity.BodyBean.GuestsBean> listGuests=new ArrayList<>();
    private OrderDetailsAdapter orderDetailsAdapter;
    private int mStatus = 3;
    private CommonPopupWindow mPopupDetails;
    private CommonPopupWindow mPopupDetails1;
    private OrderDetailsEntity.BodyBean mBodyBean;
    private TextView tvGuideMore;
    private Handler handler;
    private long time = -1;
    private LoadDialog loadDialog;
    Gson gson=new GsonBuilder().create();
    private OrderDetailsEntity orderDetailsEntity;

    @Override
    protected int getViewResId() {
        return R.layout.activity_order_details;
    }

    @Override
    protected void init() {
        super.init();

        Bundle extras = getIntent().getExtras();
        if (extras != null) {
//            mOrderNo = extras.getString("orderNo");
            mOrderNo = extras.getString("orderNo");
        }

        ToolBarUtil.initToolBar(toolbarText, "订单详情", new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        initRecycle();


       // getOrderDetails();
    }

    @Override
    public void onResume() {
        super.onResume();
        loadDialog = LoadDialog.newInstance("加载中");
        loadDialog.show(getSupportFragmentManager(), "loading");
        listGuests.clear();
        getOrderDetails();
    }

    private void initRecycle() {
        rvOrderUserLeft.setLayoutManager(new LinearLayoutManager(this, LinearLayout.VERTICAL, false));
        orderDetailsAdapter = new OrderDetailsAdapter(this);
        rvOrderUserLeft.setAdapter(orderDetailsAdapter);
        orderDetailsAdapter.setData(listGuests);
    }

    private void getOrderDetails() {
        Map<String, String> map = new HashMap<>();
        map.put("orderNo", mOrderNo);
        OkHttpUtil.getJSON(this, Constant.URL.orderDetails, map, new OkHttpUtil.OnDataListener() {

            @Override
            public void onResponse(String url, String json) {
                dismissDialog();
                try {
                    LogUtil.jLog().e(json);
                    orderDetailsEntity = new Gson().fromJson(json, OrderDetailsEntity.class);
                    if (orderDetailsEntity.getCode().equals(Constant.Strings.SUC)) {
                        if (orderDetailsEntity.getBody().getInvoice()==null){
                            if (orderDetailsEntity.getBody().isAllowInvoice()) {
                                ll_order_fapiaoInfo.setVisibility(View.VISIBLE);
                            }else {
                                ll_order_fapiaoInfo.setVisibility(View.GONE);

                            }

                            tv_order_fapiao.setText("发票申请");
                            tv_fapiaoName.setVisibility(View.GONE);
                            tv_type.setVisibility(View.GONE);

                        }else {
                            ll_order_fapiaoInfo.setVisibility(View.VISIBLE);

                            tv_order_fapiao.setText(orderDetailsEntity.getBody().getInvoice().getState().getText());
                            tv_fapiaoName.setText(orderDetailsEntity.getBody().getInvoice().getTitle());
                            tv_type.setText(orderDetailsEntity.getBody().getInvoice().getType());
                            tv_fapiaoName.setVisibility(View.VISIBLE);
                            tv_type.setVisibility(View.VISIBLE);
                        }
                        mBodyBean = orderDetailsEntity.getBody();
                        tvOrderContentTitle.setText(mBodyBean.getProduct().getName());

                        if (mBodyBean.getProduct().getNameEn() != null) {
                            tvOrderContentEng.setText(mBodyBean.getProduct().getNameEn());
                        } else {
                            tvOrderContentEng.setText(mBodyBean.getProduct().getName());
                        }

                        if (mBodyBean.isAllowInvoice()) {
                            ll_order_fapiaoInfo.setVisibility(View.VISIBLE);
                        } else {
                            ll_order_fapiaoInfo.setVisibility(View.GONE);

                        }
                        int dateDays = CalendarUtil.getDateDays(mBodyBean.getCheckOutDate(), mBodyBean.getCheckInDate());
                        tvOrderContentDay.setText(dateDays + "晚");
                        initType(mBodyBean.getOrderStatus().getName(), mBodyBean.getOrderStatus().getContent());
                        tvOrderContentLeft.setText(mBodyBean.getProduct().getChildProduct().getRoomName());
                        tvOrderContentNum.setText(mBodyBean.getGuests().size() + "间");

                        tvOrderContentTime.setText(CalendarUtil.FormatChangeString(mBodyBean.getCheckInDate()) + " - " + CalendarUtil.FormatChangeString(mBodyBean.getCheckOutDate()));
                        tvOrderUserPhone.setText(mBodyBean.getContact().getMobile());

                        if (mBodyBean.getGuests().get(0).getEmail() != null && mBodyBean.getGuests().get(0).getEmail().length() > 0) {
                            tvOrderUserEmail.setText(mBodyBean.getGuests().get(0).getEmail());
                        } else {
                            tvOrderEmailLeft.setVisibility(View.GONE);
                            tvOrderUserEmail.setVisibility(View.GONE);
                        }

                        tvOdMoney.setText(mBodyBean.getActualPriceValue());

                        tvOrderBottomNum.setText(mBodyBean.getOrderNo());

                        if (mBodyBean.getPaymentMethod() == 2) {
                            tvOrderBottomType.setText("微信支付");
                        } else if (mBodyBean.getPaymentMethod() == 3) {
                            tvOrderBottomType.setText("支付宝支付");
                        } else {
                            tvOrderBottomType.setText("在线支付");
                        }

                        tvOrderBottomTime.setText(mBodyBean.getCreateTime());
                        if (mBodyBean.getRemarks() != null && mBodyBean.getRemarks().length() != 0) {
                            tvOrderBottomMore.setText(mBodyBean.getRemarks());
                        } else {
                            tvOrderBottomMore.setVisibility(View.GONE);
                            tvOrderBottomMoreLeft.setVisibility(View.GONE);
                        }

                        tvOdDetailsDesc.setText(mBodyBean.getProduct().getChildProduct().getRules());

                        if (mBodyBean.isAllowCancel()) {
                            tvOrderDelete.setVisibility(View.VISIBLE);
                        }

                        if (mStatus == 3 && mBodyBean.isAllowPayment()) {
                            tvOrderPay.setVisibility(View.VISIBLE);
                            tvOrderPay.setText("去支付");
                        } else {
                            tvOrderPay.setText("再次预订");
                        }

                        if (mStatus != 3 && mBodyBean.isAllowBuy()) {
                            tvOrderPay.setVisibility(View.VISIBLE);
                        }


                        if (mBodyBean.isAllowBespeak()) {
                            tvOrderPhone.setVisibility(View.VISIBLE);
                        }


                        if (mBodyBean.getGuests().size() > 3) {
                            tvOdMore.setVisibility(View.VISIBLE);
                            vwOdMoreBottom.setVisibility(View.VISIBLE);
                            if (mBodyBean.getGuests().get(0).getIdcard() == null) {
                                vwOdMoreTop.setVisibility(View.VISIBLE);
                            }
                            for (int i = 0; i < 3; i++) {
                                listGuests.add(mBodyBean.getGuests().get(i));
                            }
                        } else {
                            listGuests = mBodyBean.getGuests();
                            tvOdMore.setVisibility(View.GONE);
                            vwOdMoreBottom.setVisibility(View.GONE);
                        }
                        orderDetailsAdapter.setData(listGuests);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(String url, String error) {
                dismissDialog();

            }
        });
    }


    public void initType(String title, String content) {
//        0：待入住，1:已取消,2:待确认,3:待预约,4:待入住,5:已入住,6:已退房,7:退款中,8:已退款,9:已派送,10:待派送"
        try {
            if (time != -1) {
                time = 0;
            }
            tvOrderRight.setVisibility(View.GONE);
            switch (title) {
                case "已取消"://已取消
                    ivOrderType.setImageResource(R.drawable.order_status_cancelled);
                    tvOrderDelete.setVisibility(View.GONE);
                    break;
                case "待使用"://待使用
                    tvOrderDelete.setVisibility(View.GONE);
                    ivOrderType.setImageResource(R.drawable.order_status_to_be_used);
                    break;
                case "待支付"://待支付
                    tvOrderRight.setVisibility(View.VISIBLE);
                    ivOrderType.setImageResource(R.drawable.order_status_to_be_paid);
                    tvOrderDelete.setVisibility(View.VISIBLE);
                    time = mBodyBean.getOrderStatus().getRemainingPaymentTime();
                    handler = new Handler();
                    Runnable runnable = new Runnable() {
                        @Override
                        public void run() {
                            if (handler != null) {
                                tvOrderRight.setText(CalendarUtil.FormatDateLong(time));
                                if (time <= 0) {
                                    tvOrderRight.setVisibility(View.GONE);
                                    handler = null;
                                } else {
                                    time = time - 1000;
                                    handler.postDelayed(this, 1000);
                                }
                            }
                        }
                    };
                    handler.post(runnable);
                    break;
                case "确认中"://确认中
                    tvOrderDelete.setVisibility(View.GONE);
                    ivOrderType.setImageResource(R.drawable.order_status_confirming);
                    break;
                case "已完成"://已入住
                    tvOrderDelete.setVisibility(View.GONE);
                    ivOrderType.setImageResource(R.drawable.order_status_completed);
                    break;
                case "已退款"://已退款
                    tvOrderDelete.setVisibility(View.GONE);
                    ivOrderType.setImageResource(R.drawable.order_status_refunded);
                    break;
                case "退款中"://退款中
                    tvOrderDelete.setVisibility(View.GONE);
                    ivOrderType.setImageResource(R.drawable.order_status_refunding);
                    break;

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        tvOrderTitle.setText(title);
        tvOrderContent.setText(content);
    }


    /**
     * 点击事件
     *
     * @param view
     */
    @OnClick({R.id.tv_order_fapiaoInfo, R.id.tv_order_fapiao, R.id.tv_order_bottom_num_right, R.id.tv_order_content_right, R.id.tv_order_more
            , R.id.iv_order_more, R.id.tv_order_phone, R.id.tv_order_pay, R.id.tv_order_delete
            , R.id.tv_od_more})
    public void btnOnclick(View view) {
        switch (view.getId()) {
            case R.id.tv_order_fapiaoInfo://发票弹窗
                loadDialog = LoadDialog.newInstance("加载中");
                loadDialog.show(getSupportFragmentManager(), "loading");                HashMap<String, String> map = new HashMap<>();
                map.put("key","invoice_info");
                OkHttpUtil.getJSON(this, Constant.URL.faPiaoconfigurations, map, new OkHttpUtil.OnDataListener() {
                    @Override
                    public void onResponse(String url, String json) {
                        dismissDialog();
                        if (json!=null){
                            LogeUtil.e("faPiaoconfigurations",json);
                            FaPiaoconfigurationsEntity faPiaoconfigurationsEntity = gson.fromJson(json, FaPiaoconfigurationsEntity.class);
                            if (Constant.Strings.SUC.equals(faPiaoconfigurationsEntity.getCode())){
                                FaPiaoInfoPop faPiaoInfoPop = new FaPiaoInfoPop(OrderDetailsActivity.this, OrderDetailsActivity.this,faPiaoconfigurationsEntity.getBody().getContent());
                                faPiaoInfoPop.showPopupWindow();
                            }
                        }

                    }

                    @Override
                    public void onFailure(String url, String error) {
                        dismissDialog();
                    }
                });






                break;
            case R.id.tv_order_fapiao: {//发票

                if (!TextUtils.isEmpty(mOrderNo)&&orderDetailsEntity!=null&&orderDetailsEntity.getBody()!=null) {

                    if (orderDetailsEntity.getBody().getInvoice()!=null){

                        Intent intent = new Intent(this, CheckInvoiceActivity.class);

                        intent.putExtra("orderNo", getIntent().getStringExtra("orderNo"));
                        startActivity(intent);
                    }else {
                        Intent intent = new Intent(this, InvoiceApplicationActivity.class);

                        intent.putExtra("OrderNo", getIntent().getStringExtra("orderNo"));
                        startActivity(intent);
                    }
                }



            }
            break;

            case R.id.tv_order_bottom_num_right://复制
                //获取剪贴板管理器：
                ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                // 创建普通字符型ClipData
                ClipData mClipData = ClipData.newPlainText("Label", tvOrderBottomNum.getText().toString());
                // 将ClipData内容放到系统剪贴板里。
                cm.setPrimaryClip(mClipData);

                ToastUtil.initToast(this, "已经复制到粘贴板");
                break;
            case R.id.tv_order_content_right://房型详情
                initPopupMore();
                break;
            case R.id.tv_order_more:
            case R.id.iv_order_more:
                initPopupDetails();
                break;
            case R.id.tv_order_delete://取消订单
                ConfirmReturnDialog confirmDialog = ConfirmReturnDialog.newInstance("取消提醒", "确定要取消订单么？", "再想想", "确定");
                confirmDialog.setOnBtnClickListener(new ConfirmReturnDialog.OnBtnClickListener() {
                    @Override
                    public void onBtnClick(View view) {
                        switch (view.getId()) {
                            case R.id.btn_cancelConfirm:

                                break;
                            case R.id.btn_deleteConfirm:
                                ChangeDetailsPostEntity changeDetailsPostEntity = new ChangeDetailsPostEntity();
                                changeDetailsPostEntity.setAction(2);
                                changeDetailsPostEntity.setOrderNo(mBodyBean.getOrderNo());
                                Map<String, Object> map = new HashMap<>();
                                OkHttpUtil.postJson(OrderDetailsActivity.this, Constant.URL.changeDetails, changeDetailsPostEntity, map, new OkHttpUtil.OnDataListener() {

                                    @Override
                                    public void onResponse(String url, String json) {
                                        try {
                                            MemberGuestChangeEntity memberGuestChangeEntity = new Gson().fromJson(json, MemberGuestChangeEntity.class);
                                            if (memberGuestChangeEntity.getCode().equals(Constant.Strings.SUC)) {
                                                getOrderDetails();
                                            } else {
                                                ToastUtil.initToast(OrderDetailsActivity.this, memberGuestChangeEntity.getMsg());
                                            }
                                        } catch (Exception e) {
                                            e.printStackTrace();
                                        }
                                    }

                                    @Override
                                    public void onFailure(String url, String error) {

                                    }
                                });
                                break;
                        }
                    }

                    @Override
                    public void onDeselect(View view) {

                    }
                });
                confirmDialog.show(getSupportFragmentManager(), "返回");
                break;
            case R.id.tv_order_phone://联系客服
                Intent intentPhone = new Intent(Intent.ACTION_DIAL);//调出拨号界面
                intentPhone.setData(Uri.parse("tel:" + mBodyBean.getContactMobile()));
                startActivity(intentPhone);
                break;
            case R.id.tv_order_pay://再次预订
                if (tvOrderPay.getText().toString().equals("去支付")) {
                    pay();
                } else {
                    Bundle bundle = new Bundle();
                    bundle.putString("hotelId", mBodyBean.getProduct().getId());
                    startActivity(WineShopDetailsActivity.class, bundle);
                }
                break;
            case R.id.tv_od_more://查看更多
                tvOdMore.setVisibility(View.GONE);
                vwOdMoreBottom.setVisibility(View.GONE);
                vwOdMoreTop.setVisibility(View.GONE);
                listGuests = mBodyBean.getGuests();
                orderDetailsAdapter.setData(listGuests);
                break;
        }
    }

    public void pay() {
        final PayDialog payDialog = PayDialog.newInstance(mBodyBean.getActualPriceValue());
        payDialog.show(getFragmentManager());
        payDialog.setOnItemClickListener(new BaseDialog.OnItemClickListener() {
            @Override
            public void onItemClick(View v) {
                SharedPreferencesUtil.saveWeOrderBoolean(OrderDetailsActivity.this, true);
                SharedPreferencesUtil.saveWeOrderNum(OrderDetailsActivity.this, mBodyBean.getOrderNo());
                Map<String, Object> objectObjectHashMap = new HashMap<>();
                OkHttpUtil.postJson(OrderDetailsActivity.this, Constant.URL.pay, new PayMent(mBodyBean.getOrderNo(), 2), objectObjectHashMap, new OkHttpUtil.OnDataListener() {
                    @Override
                    public void onResponse(String url, String json) {
                        if (!TextUtils.isEmpty(json)) {
                            LogeUtil.e("pay11", json);
                            PayEntity payEntity = new Gson().fromJson(json, PayEntity.class);
                            if ("200".equals(payEntity.getCode())) {
                                IWXAPI api = WXAPIFactory.createWXAPI(OrderDetailsActivity.this, Constant.ID.WeChat_APP_ID, true);
                                api.registerApp(Constant.ID.WeChat_APP_ID);
                                if (!api.isWXAppInstalled()) {//?????
                                    ToastUtil.initToast(OrderDetailsActivity.this, "??????");
                                    return;
                                }
                                PayEntity.BodyEntity body = payEntity.getBody();
                                PayReq req = new PayReq();
                                //req.appId = "wxf8b4f85f3a794e77";  // ??????appId
                                req.appId = body.getAppid();
                                req.partnerId = body.getPartnerid();
                                req.prepayId = body.getPrepayid();
                                req.nonceStr = body.getNoncestr();
                                req.timeStamp = body.getTimestamp();
                                req.packageValue = body.getPackageX();
                                req.sign = body.getSign();
                                req.extData = "app data";
                                // ?????????????????????????????????IWXMsg.registerApp???????????
                                api.sendReq(req);
                                onBackPressed();
                            } else {
                                ToastUtil.initToast(OrderDetailsActivity.this, payEntity.getMsg());
                            }


                        }
                    }

                    @Override
                    public void onFailure(String url, String error) {

                    }
                });

            }
        });
    }

    /**
     * 初始化popup
     */
    private void initPopupMore() {
        mPopupDetails = new CommonPopupWindow(OrderDetailsActivity.this, R.layout.popu_house_type, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) {

            @Override
            protected void initView() {
                View view = getContentView();
                view.findViewById(R.id.tv_ht_confirm).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mPopupDetails.getPopupWindow().dismiss();
                    }
                });
                view.findViewById(R.id.tv_dismiss).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mPopupDetails.getPopupWindow().dismiss();
                    }
                });

                LinearLayout llPopTitle = (LinearLayout) view.findViewById(R.id.ll_pop_title);
                TextView tvHtTitle = (TextView) view.findViewById(R.id.tv_ht_title);
                TextView tvTitle = (TextView) view.findViewById(R.id.tv_pop_title);
                TextView tvTitleRight = (TextView) view.findViewById(R.id.tv_pop_title_right);
                TextView tvTitleContent = (TextView) view.findViewById(R.id.tv_pop_title_content);

                TextView tvPopEat = (TextView) view.findViewById(R.id.tv_pop_eat);
                TextView tvEatContent = (TextView) view.findViewById(R.id.tv_pop_eat_content);
                TextView tvBed = (TextView) view.findViewById(R.id.tv_pop_bed);
                TextView tvBedContent = (TextView) view.findViewById(R.id.tv_pop_bed_content);


                tvHtTitle.setText(mBodyBean.getProduct().getChildProduct().getRoomName());
                tvTitle.setText(mBodyBean.getProduct().getChildProduct().getSalePlanName());

                tvTitleRight.setText("(" + mBodyBean.getProduct().getChildProduct().getBaseDesc().getName() + ")");
                tvTitleContent.setText(mBodyBean.getProduct().getChildProduct().getBaseDesc().getContent());

                tvPopEat.setText(mBodyBean.getProduct().getChildProduct().getFoodDesc().getName());
                String subName = mBodyBean.getProduct().getChildProduct().getFoodDesc().getName().length() == 0 ?
                        mBodyBean.getProduct().getChildProduct().getFoodDesc().getSubName() :
                        mBodyBean.getProduct().getChildProduct().getFoodDesc().getName();
                tvEatContent.setText(subName);

                tvBed.setText(mBodyBean.getProduct().getChildProduct().getBedDesc().getName());
                tvBedContent.setText(mBodyBean.getProduct().getChildProduct().getBedDesc().getContent());

                RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.rv_pop);
                recyclerView.setLayoutManager(new GridLayoutManager(OrderDetailsActivity.this, 2));


                List<OrderDetailsEntity.BodyBean.ProductBean.ChildProductBean.FacilitiesBean> filters = mBodyBean.getProduct().getChildProduct().getFacilities();
                if (filters != null && filters.size() > 0) {
                    PopupOrderAdapter popularHouseAdapter = new PopupOrderAdapter(OrderDetailsActivity.this);
                    recyclerView.setAdapter(popularHouseAdapter);
                    popularHouseAdapter.setData(filters);
                } else {
                    view.findViewById(R.id.tv_pop_base_title).setVisibility(View.GONE);
                }


                if (null != mBodyBean.getProduct().getChildProduct().getImageURLs()
                        && mBodyBean.getProduct().getChildProduct().getImageURLs().size() > 0) {
                    //Banner适配
                    BannerUtils bannerUtils1 = (BannerUtils) view.findViewById(R.id.bus_ht);
                    bannerUtils1.setAdapter(new BannerUtils.Adapter() {
                        @Override
                        public void fillBannerItem(BannerUtils bannerUtils, View itemView, Object model, int position) {
                            RequestOptions options = new RequestOptions()
                                    .error(R.drawable.map_img2x)
                                    .dontAnimate()
                                    .centerCrop()
                                    .placeholder(R.drawable.map_img2x);
                            Glide.with(itemView.getContext())
                                    .load(model)
                                    .apply(options)
                                    .into((ImageView) itemView);
                        }
                    });

                    List<String> urlString = new ArrayList<>();
                    List<String> idString = new ArrayList<>();
                    final List<String> imageURLs = mBodyBean.getProduct().getChildProduct().getImageURLs();
                    for (int i = 0; i < imageURLs.size(); i++) {
                        urlString.add(mBodyBean.getProduct().getChildProduct().getImageURLs().get(i));
                        idString.add("空的");
                    }
                    bannerUtils1.setData(R.layout.view_image_circle, urlString, idString);

                    tvGuideMore = (TextView) view.findViewById(R.id.tv_guide);
                    bannerUtils1.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                        @Override
                        public void onPageScrolled(int i, float v, int i1) {
                            if (v % 1 > 0.1 && imageURLs.size() > i + 1) {
                                tvGuideMore.setText(i + 1 + "/" + imageURLs.size());
                            } else {
                                tvGuideMore.setText(i + 1 + "/" + imageURLs.size());
                            }
                        }

                        @Override
                        public void onPageSelected(int i) {

                        }

                        @Override
                        public void onPageScrollStateChanged(int i) {

                        }
                    });
                } else {
                    view.findViewById(R.id.rl_bus).setVisibility(View.GONE);
                }
            }

            @Override
            protected void initEvent() {
                WindowManager.LayoutParams attributes = getWindow().getAttributes();
                attributes.alpha = 0.3f;
                getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                getWindow().setAttributes(attributes);
            }
        };
        mPopupDetails.getPopupWindow().setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams attributes = getWindow().getAttributes();
                attributes.alpha = 1.0f;
                getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                getWindow().setAttributes(attributes);
            }
        });
        mPopupDetails.showAtLocation(llOl, Gravity.BOTTOM, 0, 0);
    }


    /**
     * 初始化popup
     */
    private void initPopupDetails() {
        mPopupDetails1 = new CommonPopupWindow(OrderDetailsActivity.this, R.layout.popu_money_details, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) {

            @Override
            protected void initView() {
                View view = getContentView();

                TextView tvPmd = (TextView) view.findViewById(R.id.tv_pmd);
                TextView tvMe = (TextView) view.findViewById(R.id.tv_md_me);
                LinearLayout llMe = (LinearLayout) view.findViewById(R.id.ll_md_me);

                TextView tvBalance = (TextView) view.findViewById(R.id.tv_md_balance);
                LinearLayout llBalance = (LinearLayout) view.findViewById(R.id.ll_md_balance);


                TextView tvReduceTitle = (TextView) view.findViewById(R.id.tv_reduce_title);
                TextView tvReduce = (TextView) view.findViewById(R.id.tv_md_reduce);
                LinearLayout llReduce = (LinearLayout) view.findViewById(R.id.ll_md_reduce);
                llReduce.setVisibility(View.GONE);
                TextView tvOld = (TextView) view.findViewById(R.id.tv_md_old);
                LinearLayout llOld = (LinearLayout) view.findViewById(R.id.ll_md_old);


                TextView tvMdMoney = (TextView) view.findViewById(R.id.tv_md_money);

                TextView tvMdConfirm = (TextView) view.findViewById(R.id.tv_md_confirm);
                tvMdConfirm.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mPopupDetails1.getPopupWindow().dismiss();
                    }
                });

                tvMdMoney.setText(mBodyBean.getActualPriceValue());

                RecyclerView rvDetails = (RecyclerView) view.findViewById(R.id.rv_md_details);


                if (mBodyBean.getDiscountAmountValue() != null && !mBodyBean.getDiscountAmountValue().equals("0")
                        && !mBodyBean.getDiscountAmountValue().equals("")) {
                    tvMe.setText(Html.fromHtml("- &yen;" + mBodyBean.getDiscountAmountValue()));
                    llMe.setVisibility(View.VISIBLE);
                } else {
                    llMe.setVisibility(View.GONE);
                }

                if (mBodyBean.getWalletDeductibleAmountValue() != null && !mBodyBean.getWalletDeductibleAmountValue().equals("0")
                        && !mBodyBean.getWalletDeductibleAmountValue().equals("")) {
                    tvBalance.setText(Html.fromHtml("- &yen;" + mBodyBean.getWalletDeductibleAmountValue()));
                    llBalance.setVisibility(View.VISIBLE);
                } else {
                    llBalance.setVisibility(View.GONE);
                }


                if (llBalance.getVisibility() == View.VISIBLE || llMe.getVisibility() == View.VISIBLE) {
                    tvReduceTitle.setVisibility(View.VISIBLE);
                } else {
                    tvReduceTitle.setVisibility(View.GONE);
                }

                llReduce.setVisibility(View.GONE);
                llOld.setVisibility(View.GONE);

                List<OrderDetailsEntity.BodyBean.ProductBean.ChildProductBean.PriceListBean> priceList = mBodyBean.getProduct().getChildProduct().getPriceList();
                List<DataEntity> list = new ArrayList<>();
                for (int i = 0; i < priceList.size(); i++) {
                    DataEntity dataEntity = new DataEntity();
                    dataEntity.setTitle(priceList.get(i).getDayValue());
                    dataEntity.setContent(priceList.get(i).getPriceValue());
                    list.add(dataEntity);
                }
                rvDetails.setLayoutManager(new LinearLayoutManager(OrderDetailsActivity.this, LinearLayoutManager.VERTICAL, false));
                OrderDeatailsPopuAdapter orderDeatailsPopuAdapter = new OrderDeatailsPopuAdapter(OrderDetailsActivity.this);
                rvDetails.setAdapter(orderDeatailsPopuAdapter);
                orderDeatailsPopuAdapter.setData(list);
            }

            @Override
            protected void initEvent() {
                WindowManager.LayoutParams attributes = getWindow().getAttributes();
                attributes.alpha = 0.3f;
                getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                getWindow().setAttributes(attributes);
            }
        };
        mPopupDetails1.getPopupWindow().setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams attributes = getWindow().getAttributes();
                attributes.alpha = 1.0f;
                getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                getWindow().setAttributes(attributes);
            }
        });
        mPopupDetails1.showAtLocation(llOl, Gravity.BOTTOM, 0, 0);
    }


    /**
     * 停止加载框
     */
    protected void dismissDialog() {
        if (loadDialog != null) {
            loadDialog.dismiss();//获取信息后关闭对话框
        }
    }

    /**
     * 设置加载框
     */
    protected void showDialog() {
        if (loadDialog == null) {
            loadDialog = LoadDialog.newInstance("加载中");

        }
        loadDialog.show(getSupportFragmentManager(), "loading");
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (handler != null) {
            handler.removeCallbacksAndMessages(0);
            handler = null;
        }
    }
}

